using Microsoft.AspNetCore.Mvc;
using Lzfy_Refund_Service.Models;
using Lzfy_Refund_Service.Services;

namespace Lzfy_Refund_Service.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class AuthController : ControllerBase
    {
        private readonly IAuthService _authService;

        public AuthController(IAuthService authService)
        {
            _authService = authService;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="request">登录请求</param>
        /// <returns>登录响应，包含JWT Token</returns>
        [HttpPost("login")]
        public async Task<ActionResult<LoginResponse>> Login([FromBody] LoginRequest request)
        {
            Console.WriteLine($"Login attempt - Username: {request?.Username}, Password: {request?.Password}");
            
            if (request == null || string.IsNullOrEmpty(request.Username) || string.IsNullOrEmpty(request.Password))
            {
                Console.WriteLine("Login failed - Empty username or password");
                return BadRequest(new { message = "用户名和密码不能为空" });
            }

            var response = await _authService.LoginAsync(request);
            if (response == null)
            {
                Console.WriteLine("Login failed - Invalid credentials");
                return Unauthorized(new { message = "用户名或密码错误" });
            }

            Console.WriteLine("Login successful");
            return Ok(response);
        }
    }
}